﻿using System;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Web;

namespace TeachingAffairsManagementSystem
{
    public static class Utils
    {
        public static string SendEmail(string sto, string sToSubject, string sContent)
        {
            string sRestring = "";
            try
            {
                string sSmtp = "smtp.qq.com";
                string sPort = "25";
                string sFrom = "brother_b@qq.com";
                string sAccount = "brother_b@qq.com";
                string sPass = "nhipzejdpzsrbcfc"; //这个密码切记一定要qq邮箱里面生成的授权码！授权码 ！授权码 ！

                System.Net.Mail.SmtpClient client = new SmtpClient();
                client.Host = sSmtp;
                client.UseDefaultCredentials = false;
                client.Port = Convert.ToInt16(sPort);
                client.Credentials = new System.Net.NetworkCredential(sAccount, sPass);

                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                System.Net.Mail.MailMessage message = new MailMessage(sFrom, sto);
                message.Subject = sToSubject;
                message.Body = sContent;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;
                ///message.EnableSsl = false;


                client.Send(message);

                //==================================End邮件发布===========================

                sRestring = "已发送";
            }
            catch (Exception ex)
            {
                sRestring = ex.Message.ToString();
            }

            return sRestring;
        }

        public static string GenerateGuid()
        {
            return Guid.NewGuid().ToString();
        }

        /// <summary>
        /// 判断输入是否为邮箱
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsEmail(string inputData)
        {
            if (inputData=="")
            {
                return false;
            }
            Regex regEmail =
                new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            Match m = regEmail.Match(inputData);
            return m.Success;
        }

        public static void OpenWindow(string url)
        {
            HttpContext.Current.Response.Write($"<script>window.open('{url}');</script>");
        }
    }
}